<?php

namespace app\interfaces\model;

use app\interfaces\model\Shop as ShopModel;
use app\interfaces\model\MemberOrder as MemberOrderModel;
use think\Model;
use think\Request;

class MemberRefund extends Model
{

    // 店铺标题
    public function getShopTitleAttr($value, $data)
    {
        $shop_id = MemberOrderModel::where(['shop_order_number' => $data['order_number']])->value('shop_id');
        return (new ShopModel())->where(['member_id' => $shop_id])->value('title');
    }

    // 店铺id
    public function getShopIdAttr($value, $data)
    {
        return MemberOrderModel::where(['shop_order_number' => $data['order_number']])->value('shop_id');
    }

    // 实付款
    public function getGoodsPriceAttr($value, $data)
    {
        return (new MemberOrderModel())->where(['shop_order_number' => $data['order_number']])->value('price');
    }

    // 列表
    public function goodsList()
    {
        $information['id'] = ['gt', 0];

        $title = Request::instance()->param('title');

        if (!empty($title)) $information['title'] = ['like', '%' . $title . '%'];

        return $this->hasMany('MemberOrderAttached', 'shop_order_number', 'order_number')
            ->where($information)
            ->field('goods_id,title,thumb_img,number,price as goods_price,attr,rebate_percentage');
    }


    // 订单状态
    public function memberOrder()
    {
        return $this->hasOne('MemberOrder','shop_order_number','order_number')->field('order_number,status');
    }

    // 申请时间
    protected function getCreateTimeAttr($value, $data)
    {
        return date('Y-m-d H:i:s', $data['create_time']);
    }

    // 退款时间
    protected function getRefundTimeAttr($value, $data)
    {
        return date('Y-m-d H:i:s', $data['refund_time']);
    }

    // 模型事件
    protected static function init()
    {
        // 新增前
        self::event('before_insert', function ($data) {
            $data->member_id = Request::instance()->param('user_id');
            $data->picArr = multi_upload();
            $data->create_time = time();
        });
    }
}
